﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 图片处理测试
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        cat_pic_hash cat_hash = new cat_pic_hash();//计算hash的类

        private void button1_Click(object sender, EventArgs e)

        {
            cat_pic_compression cat_compression = new cat_pic_compression();
       
               String sFile = ".\\"+textBox1.Text;
            String dFile = ".\\"+ textBox2.Text;
            Console.WriteLine("压缩"+cat_compression.CompressImage(sFile, dFile, 90, 300, true));//测试压缩的函数
            Console.WriteLine(GetMD5WithFilePath(sFile));//获取md5的函数，不再单独抽出去

        }








        static public string GetMD5WithFilePath(string filePath)
        {
            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hash_byte = md5.ComputeHash(file);
            string str = System.BitConverter.ToString(hash_byte);
            str = str.Replace("-", "");//如果不替换那个讨厌的-，就会变成很长的字符串
            return str;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            Image SourceImg;

            String sFile = ".\\" + textBox1.Text;
            String dFile = ".\\" + textBox2.Text;
            SourceImg = Image.FromFile(sFile);
            pictureBox1.Image = SourceImg;
            String first = cat_hash. GetHash(SourceImg);

            Console.WriteLine("" + first); ;
            label4.Text = "第1张图片指纹：" + first;
            SourceImg = Image.FromFile(dFile);

            String second = cat_hash. GetHash(SourceImg);
            Console.WriteLine("" + second); ;
            label5.Text = "第2张图片指纹：" + first;
            pictureBox2.Image = SourceImg;
            StringCompute stringcompute1 = new StringCompute();
            stringcompute1.SpeedyCompute(first, second);    // 计算相似度， 不记录比较时间
            decimal rate = stringcompute1.ComputeResult.Rate;         // 相似度百分之几，完全匹配相似度为1
            Console.WriteLine(rate);
            label1.Text="相似度:"+rate;
        }


    
    }
}
